package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.pojo.HelperCate;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.HelperCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 帮助中心类别
 * @Author zhangjin
 * @Date 2022/8/29 11:32
 */
@RestController
public class HelperCateController {
    @Autowired
    private HelperCateService helperCateService;

    /**
     * 新增类别
     * @param helperCate
     * @return
     */
    @RequestMapping("/admin/addHelperCat")
    @PreAuthorize("hasAnyAuthority('helper:cateAdd')")
    public ResponseEntity addCate(@RequestBody @Valid HelperCate helperCate){
        helperCateService.add(helperCate);
        return ResponseEntity.SUCCESS("添加成功",null);
    }

    /**
     * 修改帮助中心类别
     * @param cate
     * @return
     */
    @PostMapping("/admin/updateCate")
    @PreAuthorize("hasAnyAuthority('helper:cateEdit')")
    public ResponseEntity updateCate(@RequestBody @Valid HelperCate cate){
        helperCateService.update(cate);
        return ResponseEntity.SUCCESS("修改成功",null);
    }

    @PostMapping("/admin/delCate")
    @PreAuthorize("hasAnyAuthority('helper:cateDel')")
    public ResponseEntity delCate(@RequestBody HelperCate cate){
        helperCateService.removeById(cate.getId());
        return ResponseEntity.SUCCESS("删除成功",null);
    }
    /**
     * 返回所有的类别：
     * @return
     */
    @PostMapping("/admin/catelist")
    @PreAuthorize("hasAnyAuthority('helper:cateList')")
    public ResponseEntity getCateList(@RequestBody PageBean pageBean){
        if (pageBean.getCurrPage()==0&&pageBean.getPageSize()==0){
            return ResponseEntity.SUCCESS("操作成功",helperCateService.list());
        }else{
            return ResponseEntity.SUCCESS("操作成功",helperCateService.getCateByPage(pageBean));
        }
    }

    /**
     * 获取所有的类别
     * @return
     */
    @RequestMapping("/getAllCate")
    public ResponseEntity<List<HelperCate>> getAllCate(){
        List<HelperCate> list = helperCateService.getAllCate();
        return ResponseEntity.SUCCESS("ok",list);
    }
    /**
     * 批量删除
     */
    @PostMapping("/admin/delAllCate")
    @PreAuthorize("hasAnyAuthority('helper:cateDel')")
    public ResponseEntity delAllSelect(@RequestBody int[] ids){
        List<Integer> idList = new ArrayList<>();
        for (int id : ids) {
            idList.add(id);
        }
        return ResponseEntity.SUCCESS("删除成功",helperCateService.removeBatchByIds(idList));
    }
}
